<%@ page import="org.biojava.services.das.dao.RegistryUser" %>
<%@ page import="org.biojava.services.das.registry.UserManager" %>
<%@ page import="org.biojava.services.das.dao.Role" %>
<%@ page import="org.biojava.services.das.dao.RegistryUser" %>
<%@ page import="org.biojava.services.das.dao.ProjectBean"%>
<jsp:directive.page import="org.biojava.services.das.registry.DasRegistrySql" />
<jsp:directive.page import="org.biojava.dasobert.dasregistry.DasSource" />
<!--<jsp:useBean id="coord" class="dasregistry.coordBean" scope="page"/>-->
<!--<jsp:useBean id="register" class="dasregistry.registryBean"	scope="page" />-->
<%@ page import="java.util.List,java.util.ArrayList,java.util.Iterator"%>
<jsp:directive.page session="true" />



<% String title="Add DAS source to Project";
   String header = title;
%>

<%@ include file="sangerheader.jsp" %>


<% 	
	if ( email == null) {		
		response.sendRedirect("login.jsp");	
		return;
	}

%>	
	
<% 
	String[] projectIDS = request.getParameterValues("projectID");
	String[] dsIDS      = request.getParameterValues("DS_ID");

	int project_ID = -1;
	ProjectBean project = null;
	List dasSources = new ArrayList();
	

/* Prepare the data for this page */

		if (projectIDS != null){
			if ( projectIDS.length > 0 ){
				try {
					project_ID = Integer.parseInt(projectIDS[0]);
					project = coord.getProject(project_ID);
				} catch (Exception e){
					out.print(e.getMessage());
				}						
			}						
		}

		if (dsIDS != null){
		for ( int i=0;i< dsIDS.length;i++){
			String dsID = dsIDS[i];
			DasSource ds = register.getDasSource(dsID);
			dasSources.add(ds);
			
		}
	}
	%>


<div id="main">

<h2>Add DAS sources to project</h2>

<%

///////////////////////////////
//
//display choose project form
//
///////////////////////////////

if (project == null) {

	ProjectBean[] projects = coord.getAllProjects();
%> 
<form id="projectaction" name="projectaction" action="ProjectAction" method="POST" >
<input type="hidden" name="action" value="add"/>

<%@ include file="sangertablestart.jsp" %>
<%
	Iterator iter = dasSources.iterator();
	int count=-1;
	while (iter.hasNext()){
		count++;
		DasSource ds = (DasSource) iter.next();
		%>
<input type="hidden" name="DS_ID" value="<%=ds.getId()%>"/>		
<% if (count % 2 != 0) {
%>
<tr id="row1" valign="top">
	<%
	} else {
	%>

<tr id="row2" valign="top">
<%} %>

<td><%=ds.getId() %></td>
<td><%=ds.getNickname() %></td>
<td><%=ds.getUrl() %></td>
<td><%
	ProjectBean tp = coord.getProjectBeanForURL(ds.getUrl());
	if (tp != null) {
		%><img src="<%= tp.getIconURL()%>"/><%
	}
%></td>
</tr>
		<%
	}
%>
<%@ include file="sangertableend.jsp" %>

<h3>Choose your project:</h3>

<% if (projects.length == 0) {
	%>
	You have not created a project, yet. <a href="registerProject.jsp">Create one here</a>.
	<% 
}
%>
<%@ include file="sangertablestart.jsp" %>

<% 
	if ( user != null) {%>
<tr id="row0">
<td>&nbsp;</td>
<% } %>
<td>Icon</td>
<td>Name</td>
<td>Homepage</td>
<%--<td>Owner</td>--%>
<td>Description</td>
<td>Nr DAS sources</td>

</tr>
<%
for (int i=0; i<projects.length;i++){
		ProjectBean p = projects[i];
		RegistryUser owner = p.getOwner();
		if (owner == null)
			owner = new RegistryUser();
		
	%>

<%if (i % 2 != 0) {
%>
<tr id="row1" valign="top">
	<%
	} else {
	%>

<tr id="row2" valign="top">
<%} %>
	
	<% if ( user != null) {
		%><td>
		<input type="radio" name="projectID" value="<%=p.getProjectID() %>"/><%
		
		%></td><% 
	}%>
	<td><a href="showProjectDetails.jsp?project_id=<%=p.getProjectID()%>">
	<img src="ProjectIcon?id=<%=p.getProjectID()%>"/></a></td>
	<td><a href="showProjectDetails.jsp?project_id=<%=p.getProjectID()%>">
	<%=p.getName() %></a></td>
	<td><a href="<%=p.getHomepage() %>"><%=p.getHomepage() %></a></td>
	<%-- <td>< %=owner.getemail() % ></td> --%>
	<td><%=p.getDescription() %></td>
	<td><a href="showProjectDetails.jsp?project_id=<%=p.getProjectID()%>"><%= coord.getNrDasSourcesForProject(p) %></a></td>

	
	</tr>
	<% 
	}
%>
<tr><td colspan="7" align="right">
<input type="submit" value="select project"/>
</td></tr>
<%@ include file="sangertableend.jsp" %>
</form>

<%-- end of choose project form --%>
<% 
} else if ( dasSources.size() < 1 ) {
//////////////////////////////
//
//	display choose DAS source form
//
//////////////////////////////	/
	BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
		DasRegistrySql registry=(DasRegistrySql)ctx.getBean("registryBean");
	List<DasSource>	s = registry.getServicesForEmail(email);
	
%>
<form id="dassourceaction" name="dassourceaction" action="DasSourceAction" method="POST" >
<%
if ( project != null ){
%>
choose DAS sources to add to project <strong><%=project.getName() %></strong> 
<img src="<%= project.getIconURL()%>"/>:
<input type="hidden" name="projectID" value="<%=project.getProjectID()%>"/>
<%} %>


<input type="hidden" name="action" value="add" />
<%@ include file="sangertablestart.jsp" %>	
<tr>
<td>select</td>
<td>nickname</td>
<td>url</td>
<td>project</td>
</tr>
<%	
	for (int i=0;i<s.size();i++){
		DasSource ds = s.get(i);
%>
<tr>
<td><input type="checkbox" name="DS_ID" value="<%=ds.getId() %>"/></td>
<td><%=ds.getNickname()%></td>
<td><%=ds.getUrl()%></td>
<td><%
	ProjectBean tp = coord.getProjectBeanForURL(ds.getUrl());
	if (tp != null) {
		%><img src="<%= tp.getIconURL()%>"/><%
	}
%></td>
</tr>

<% 
	}
%>
<%@ include file="sangertableend.jsp" %>	
<input type="submit" value="add DAS sources"/>
</form>
<%
// end of choose DAS source form
} else {

%>

<%-- all data complete...--%>
<form id="projectaddsourcesubmit" name="projectadddassourcesubmit" action="ProjectAction" method="POST">
<input type="hidden" name="action" value="addsource"/>
Selected project:
<strong><%=project.getName() %></strong> <img src="<%= project.getIconURL()%>"/>
<input type="hidden" name="projectID" value="<%=project.getProjectID()%>"/>
<br/>
<br/>
<h2>Please confirm:</h2>
Should the following DAS sources be added to this project?
<table>
<%
Iterator iter = dasSources.iterator();
while (iter.hasNext()){
	DasSource ds = (DasSource) iter.next();
	%>
<tr>
<td><%=ds.getId() %></td>
<td><%=ds.getNickname() %></td>
<td><%=ds.getUrl() %></td>
<td><%
	ProjectBean tp = coord.getProjectBeanForURL(ds.getUrl());
	if (tp != null) {
		%><img src="<%= tp.getIconURL()%>"/><%
	}
%></td>
</tr>
<input type="hidden" name="DS_ID" value="<%=ds.getId()%>"/>
	<% 
}

%>
</table>
<input type="submit" value="add das sources"/>
</form>
<%} %>
</div>

<%@ include file="sangerfooter.jsp" %>